<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<script src="/public/static/js/jquery-3.31.min.js"></script>
<script>
    var userCode = getQueryString('userCode')||window.sessionStorage.getItem('tm_moc_code')||'';
    var componentUrl = getQueryString('componentUrl')||window.localStorage.getItem('tm_moc_componentUrl')||'';
    var state = getQueryString('state')||'';
    var tm_url = getQueryString('tm_url')||window.localStorage.getItem('tm_moc_url')||'';
    var edit = getQueryString('edit')||window.sessionStorage.getItem('tm_moc_role_edit')||'';
    var role_delete = getQueryString('delete')||window.sessionStorage.getItem('tm_moc_role_delete')||'';
    var roleCode = getQueryString('role_code')||window.sessionStorage.getItem('tm_moc_role_code')||'';
    var roleName = getQueryString('role_name')||window.sessionStorage.getItem('tm_moc_role_name')||'';
    $(document).ready(function() {
        if (userCode){
            window.sessionStorage.setItem('tm_moc_code',userCode);
        }
        if (componentUrl){
            window.localStorage.setItem('tm_moc_componentUrl',componentUrl);
        }
        if (tm_url){
            window.localStorage.setItem('tm_moc_url',tm_url);
        }
        if (getQueryString('role_code')){
            window.sessionStorage.setItem('tm_moc_role_code',roleCode);
        };
        if (getQueryString('role_name')) {
            window.sessionStorage.setItem('tm_moc_role_name',roleName);
        };
        if (getQueryString('edit')){
            window.sessionStorage.setItem('tm_moc_role_edit',edit);
        };
        if (getQueryString('delete')){
            window.sessionStorage.setItem('tm_moc_role_delete',role_delete);
        };
        if (!userCode){
            $.ajax({
                url: '/api/Mocdistribution/getDistrib_url',
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("token", localStorage.getItem("token"));
                },
                cache: false,  //禁用缓存
                dataType: 'json',
                success: function (data) {
                    if(data.code==200){
                        window.location.replace(data.data+'/public/moc/transferall.html?tm_url='+window.location.origin+'&componentUrl='+componentUrl);
                    }else{
                        var infoConfirm=confirm(data.msg)
                        if (infoConfirm==true)
                        {
                            closeWindow();
                        } else {
                            closeWindow();
                        }
                    }
                },
                error:function(){

                }
            });
            // window.location.href = 'https://nq.dmqhyadmin.com/public/moc/transferall.html?tm_url='+window.location.origin;
        }else{
            if(roleCode){
                var postData = {role_intro:'天马角色'};
                if (userCode){
                    postData.userCode = userCode;
                }
                if(state){
                    postData.state = state;
                }
                if(roleCode){
                    postData.role_code = roleCode;
                }
                if(roleName){
                    postData.role_name = roleName;
                }
                if(edit==1){
                    postData.edit = 1;
                }
                var postUrl = role_delete==1?'/api/Role/deletmocrol':'/api/Role/pushmocrol';
                $.ajax({
                    url: postUrl,//   /api/Role/pushmocrol
                    type: 'post',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("token", localStorage.getItem("token"));
                    },
                    data: postData,
                    cache: false,  //禁用缓存
                    dataType: 'json',
                    success: function (data) {
                        if(data.code>300 && data.code<310){
                            window.location.replace(data.data.moc_auth_url+(data.data.moc_auth_url.indexOf('?')>-1?'&':'?')+'tm_url='+window.location.origin+'&componentUrl='+componentUrl);
                        }else if (data.code==200) {
                            // window.sessionStorage.removeItem('tm_moc_code');
                            window.sessionStorage.removeItem('tm_moc_role_code');
                            window.localStorage.removeItem('tm_moc_componentUrl');
                            window.localStorage.removeItem('tm_moc_url');
                            window.sessionStorage.removeItem('tm_moc_role_name');
                            window.sessionStorage.removeItem('tm_moc_role_edit');
                            window.sessionStorage.removeItem('tm_moc_role_delete');
                            closeWindow();
                        }else{
                            var msg = data.data.moc_auth_url?(data.msg+',是否需要重新认证？'):data.msg;
                            var infoConfirm=confirm(msg)
                            if (infoConfirm==true)
                            {
                                if (data.data.moc_auth_url) {
                                    window.location.replace(data.data.moc_auth_url+(data.data.moc_auth_url.indexOf('?')>-1?'&':'?')+'tm_url='+window.location.origin+'&componentUrl='+componentUrl);
                                }else{
                                    closeWindow();
                                }
                            } else {
                                closeWindow();
                            }
                        }
                    },
                    error:function(){

                    }
                });
            }else{
                $.ajax({
                    url: '/system/Login/mocLogin',  //  /system/Login/mocLogin
                    type: 'post',
                    data:{
                        userCode: userCode,
                        state: state,
                    },
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("token", localStorage.getItem("token"));
                    },
                    cache: false,  //禁用缓存
                    dataType: 'json',
                    success: function (data) {
                        if (data.code==200){
                            window.localStorage.setItem('token',data.data.token);
                            window.localStorage.setItem('user_info',JSON.stringify(data.data.user_info));
                            window.localStorage.setItem('userInfo',JSON.stringify(data.data.user_info));
                            window.localStorage.setItem('userData',JSON.stringify(data.data));
                            window.localStorage.setItem('privilege',JSON.stringify(data.data.privilege));
                            window.localStorage.setItem('isAwaysLogin',1);
                            window.sessionStorage.setItem('isClickLogin',1);
                            window.sessionStorage.setItem('istoactiveKey',1);
                            window.sessionStorage.removeItem('tm_moc_code');
                            setTimeout(function () {
                                if (componentUrl){
                                    window.location.replace(window.location.origin+'/#/Module?to_url=' + encodeURIComponent(componentUrl));
                                }else{
                                    window.location.replace(window.location.origin);
                                }
                            },500);
                        }else if(data.code>300 && data.code<310){
                            window.location.replace(data.data.moc_auth_url+(data.data.moc_auth_url.indexOf('?')>-1?'&':'?')+'tm_url='+window.location.origin+'&componentUrl='+componentUrl);
                        }else{
                            var msg = data.data.moc_auth_url?(data.msg+',是否需要重新认证？'):data.msg;
                            var infoConfirm=confirm(msg)
                            if (infoConfirm==true)
                            {
                                if (data.data.moc_auth_url) {
                                    window.location.replace(data.data.moc_auth_url+(data.data.moc_auth_url.indexOf('?')>-1?'&':'?')+'tm_url='+window.location.origin+'&componentUrl='+componentUrl);
                                }else{
                                    closeWindow();
                                }
                            } else {
                                closeWindow();
                            }
                        }
                    },
                    error:function(){

                    }
                });
            }
        }
    });
    function closeWindow(){
        if (navigator.userAgent.indexOf('MSIE') > 0) { // close IE
            if (navigator.userAgent.indexOf('MSIE 6.0') > 0) {
                window.opener = null;
                window.close();
            } else {
                window.open('', '_top');
                window.top.close();
            }
        } else { // close chrome;It is effective when it is only one.
            window.opener = null;
            window.open('', '_self');
            window.close();
        }
    };
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }
</script>
</body>
</html>